<?php




namespace App\Services;


use App\Models\SiteCode;
use App\Repositories\DataStatus;
use App\Repositories\ResponseCode;
use App\Repositories\ResponseMsg;
use Illuminate\Support\Facades\Mail;
use Jiannei\Response\Laravel\Support\Facades\Response;

class Email
{
    /**
     * 发送验证码
     * @param $mobile
     * @param $type
     * @return string
     */
    public function send($email, $type=1)
    {
        $code=rand(10000,99999);
        try {
            Mail::raw('您的验证码：'.$code, function ($msg) use ($email) {
                $msg->subject('欢迎注册');
                $msg->to($email);
            });
            (new SiteCode())->createCode($email,$code,$type);
            return Response::ok(ResponseMsg::SMS_OK);
        }catch (\Exception $e){
            return Response::fail( "邮件发送失败 请稍后重试");
        }

    }

    /**
     * @param $to
     * @param $code
     * @param $type
     */
    public function check($to, $code, $type)
    {
        $result = (new SiteCode())->checkCode($to, $code, $type);
        if ($result) {
            $result->status = DataStatus::DATA_NO;
            $result->save();
            return true;
        } else {
            return false;
//            return true;
        }
    }

    /**
     * 指定模板合并数据 类型 1=注册2=登录3=修改密码4=变更资料5=其他
     * @param $type
     * @return mixed
     */
    private function content($type): array
    {
        $data['code'] = $code = rand(100000, 999999);
        $data['product'] = $product = config('api.sms.product');
        switch ($type) {
            case 1:
                $content = '验证码 ' . $code . '，您正在注册成为' . $product . '用户，感谢您的支持！';
                break;
            case 2:
                $content = '验证码 ' . $code . '，您正在登录' . $product . '，若非本人操作，请勿泄露。';
                break;
            case 3:
                $content = '验证码 ' . $code . '，您正在尝试修改' . $product . '登录密码，请妥善保管账户信息。';
                break;
            case 4:
                $content = '验证码 ' . $code . '，您正在尝试变更' . $product . '重要信息，请妥善保管账户信息。';
                break;
            case 5:
                $content = '验证码 ' . $code . '，您正在进行' . $product . '身份验证，打死不要告诉别人哦！';
                break;
        }
        $data['content'] = $content;
        return $data;
    }


    /**
     * 短信验证码错误
     * @param $k
     * @return string
     */
    protected function _status($k)
    {
        $status = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        return $status[$k];
    }
}
